package com.yhjapp.onethingoneyard.onethingoneyard;


import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017-11-28.
 */

public class RegisterLoginActivity extends AppCompatActivity {
    private TextView tv_register_Verification_Code;
    private ImageView iv_register_back;
    private ImageView iv_register_ImageCode;
    private EditText et_register_Imagecode;
    private TextView tv_register_Submit;
    CodeUtils codeUtils;
    private EditText et_register_phone;
    private EditText et_register_password;
    private EditText register_code;
    private ImageView image_register;
    boolean eyeOpen = false ;
    private ImageView iv_register_close;
    public final static String PHONE_PATTERN = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_login);
        tv_register_Verification_Code= (TextView) findViewById(R.id.tv_register_Verification_Code);
        et_register_Imagecode= (EditText) findViewById(R.id.et_register_Imagecode);
        tv_register_Submit= (TextView) findViewById(R.id.tv_register_Submit);
        et_register_phone= (EditText) findViewById(R.id.et_register_phone);
        et_register_password= (EditText) findViewById(R.id.et_register_password);
        register_code= (EditText) findViewById(R.id.register_code);
        image_register= (ImageView) findViewById(R.id.image_register);
        iv_register_close= (ImageView) findViewById(R.id.iv_register_close);
        iv_register_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final CountDownTimerUtils undown=new CountDownTimerUtils(tv_register_Verification_Code,60000,1000);
        tv_register_Verification_Code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_register_phone.getText().toString().equals("")||et_register_phone.getText().toString().equals(null)||et_register_phone.getText().toString().equals("null")){
                    Toast.makeText(RegisterLoginActivity.this,"请输入手机号", Toast.LENGTH_SHORT).show();
                }else {
                    isMatchered(PHONE_PATTERN,et_register_phone.getText().toString());
                    undown.start();
                    LoadRegisterLogincode();
                }
            }
        });
//        iv_register_back= (ImageView) findViewById(R.id.iv_register_back);
//        iv_register_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        iv_register_ImageCode= (ImageView) findViewById(R.id.iv_register_ImageCode);
        codeUtils = CodeUtils.getInstance();
        Bitmap bitmap = codeUtils.createBitmap();
        iv_register_ImageCode.setImageBitmap(bitmap);
        iv_register_ImageCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                codeUtils = CodeUtils.getInstance();
                Bitmap bitmap = codeUtils.createBitmap();
                iv_register_ImageCode.setImageBitmap(bitmap);
            }
        });

        tv_register_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String codeStr = et_register_Imagecode.getText().toString().trim();
                Log.e("codeStr", codeStr);
                if (null == codeStr || TextUtils.isEmpty(codeStr)) {
                    Toast.makeText(RegisterLoginActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String code = codeUtils.getCode();
                Log.e("code", code);
                if (code.equalsIgnoreCase(codeStr)) {
                    Toast.makeText(RegisterLoginActivity.this, "验证码正确", Toast.LENGTH_SHORT).show();
                    LoadRegisterLogin();
                } else {
                    Toast.makeText(RegisterLoginActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
//        image_register.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if ( eyeOpen ){
//                    //密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用
//                    et_register_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//                    image_register.setImageResource( R.drawable.login_icon_bukejian );
//                    eyeOpen = false ;
//                }else {
//                    //明文
//                    et_register_password.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
//                    image_register.setImageResource( R.drawable.login_icon_kejian );
//                    eyeOpen = true ;
//                }
//            }
//        });
    }


    /**
     * 正则表达式匹配判断
     * @param patternStr 匹配规则
     * @param input 需要做匹配操作的字符串
     * @return true if matched, else false
     */
    public  boolean isMatchered(String patternStr, CharSequence input) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            return true;
        }
        Toast.makeText(RegisterLoginActivity.this,"手机号格式错误，请重新输入!", Toast.LENGTH_SHORT).show();
        return false;
    }
    private void LoadRegisterLogincode(){

        String url ="http://develop.jhjvip.cn:8080/mall/mobile/login/code"+"?phone="+et_register_phone.getText().toString();
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RegisterBean list = gson.fromJson(response, RegisterBean.class);
                if(list.getCode().equals("10000")) {
                    Toast.makeText(RegisterLoginActivity.this, list.getMsg()+",请稍等...", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(RegisterLoginActivity.this, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }


        });
    }
    private void LoadRegisterLogin(){
        String userNickName=getIntent().getStringExtra("userNickName");
        String userHeadImg=getIntent().getStringExtra("userHeadImg");
        String unionid=getIntent().getStringExtra("unionid");
        final String userphone=et_register_phone.getText().toString().trim();
        String pwd=et_register_password.getText().toString().trim();
        String code=register_code.getText().toString().trim();
        if(userphone.equals("")){
            Toast.makeText(RegisterLoginActivity.this,"手机号不能为空！", Toast.LENGTH_SHORT).show();
        }else if(pwd.equals("")){
            Toast.makeText(RegisterLoginActivity.this,"密码不能为空！", Toast.LENGTH_SHORT).show();
        }else if(code.equals("")) {
            Toast.makeText(RegisterLoginActivity.this, "验证码不能为空！", Toast.LENGTH_SHORT).show();
        }else {
        String url = "http://develop.jhjvip.cn:8080/mall/mobile/login/reg" + "?userPhone=" + userphone + "&userPwd=" + pwd + "&code=" + code+"&userNickName="+userNickName+"&userHeadImg="+userHeadImg+"&unionid="+unionid;
        Log.e("WEB", "路径Notice--" + url);
            OkHttpUtils.get().url(url).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
//                    textView.setText(responseInfo.result);
                try {
                    Log.e("数据123", "数据d-------" +response);
                    Gson gson = new Gson();
                    RegisterBean list = gson.fromJson(response, RegisterBean.class);
                    if(list.getCode().equals("10000")) {
                        Toast.makeText(RegisterLoginActivity.this,"注册成功，请返回登录！", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(RegisterLoginActivity.this,LoginActivity.class);
                        intent.putExtra("phone",userphone);
                        startActivity(intent);
                        finish();
                    }else{
                        Toast.makeText(RegisterLoginActivity.this,list.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(RegisterLoginActivity.this,"未知错误", Toast.LENGTH_LONG).show();
                }
            }


        });
        }
    }


}
